Skip to main content

compio_runtime\fd\poll_fd/
mod.rs

1cfg_select! {
2    windows => {
3        #[path = "windows.rs"]
4        mod sys;
5    }
6    unix => {
7        #[path = "unix.rs"]
8        mod sys;
9    }
10    _ => {}
11}
12
13#[cfg(windows)]
14use std::os::windows::io::{AsRawSocket, RawSocket};
15use std::{io, ops::Deref};
16
17use compio_buf::IntoInner;
18use compio_driver::{AsFd, AsRawFd, BorrowedFd, RawFd, SharedFd, ToSharedFd};
19
20/// Providing functionalities to wait for readiness.
21#[derive(Debug)]
22pub struct PollFd<T: AsFd>(sys::PollFd<T>);
23
24impl<T: AsFd> PollFd<T> {
25    /// Create [`PollFd`] without attaching the source.
26    ///
27    /// Ready-based sources does not need to be attached.
28    pub fn new(source: T) -> io::Result<Self> {
29        Self::from_shared_fd(SharedFd::new(source))
30    }
31
32    /// Create [`PollFd`] from a shared file descriptor.
33    pub fn from_shared_fd(inner: SharedFd<T>) -> io::Result<Self> {
34        Ok(Self(sys::PollFd::new(inner)?))
35    }
36}
37
38impl<T: AsFd + 'static> PollFd<T> {
39    /// Wait for accept readiness, before calling `accept`, or after `accept`
40    /// returns `WouldBlock`.
41    pub async fn accept_ready(&self) -> io::Result<()> {
42        self.0.accept_ready().await
43    }
44
45    /// Wait for connect readiness.
46    pub async fn connect_ready(&self) -> io::Result<()> {
47        self.0.connect_ready().await
48    }
49
50    /// Wait for read readiness.
51    pub async fn read_ready(&self) -> io::Result<()> {
52        self.0.read_ready().await
53    }
54
55    /// Wait for write readiness.
56    pub async fn write_ready(&self) -> io::Result<()> {
57        self.0.write_ready().await
58    }
59}
60
61impl<T: AsFd> IntoInner for PollFd<T> {
62    type Inner = SharedFd<T>;
63
64    fn into_inner(self) -> Self::Inner {
65        self.0.into_inner()
66    }
67}
68
69impl<T: AsFd> ToSharedFd<T> for PollFd<T> {
70    fn to_shared_fd(&self) -> SharedFd<T> {
71        self.0.to_shared_fd()
72    }
73}
74
75impl<T: AsFd> AsFd for PollFd<T> {
76    fn as_fd(&self) -> BorrowedFd<'_> {
77        self.0.as_fd()
78    }
79}
80
81impl<T: AsFd> AsRawFd for PollFd<T> {
82    fn as_raw_fd(&self) -> RawFd {
83        self.0.as_raw_fd()
84    }
85}
86
87#[cfg(windows)]
88impl<T: AsFd + AsRawSocket> AsRawSocket for PollFd<T> {
89    fn as_raw_socket(&self) -> RawSocket {
90        self.0.as_raw_socket()
91    }
92}
93
94impl<T: AsFd> Deref for PollFd<T> {
95    type Target = T;
96
97    fn deref(&self) -> &Self::Target {
98        &self.0
99    }
100}